package com.hdax.controller;

import com.hdax.entity.*;
import com.hdax.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 面试控制器
 *
 * @author Aizen
 * @date 2022/06/02
 */
@Controller
@RequestMapping("/interview")
public class InterviewController {

    @Autowired
    private InterviewService interviewService;

    @Autowired
    private DemandService demandService;

    @Autowired
    private EmployeeInfoService employeeInfoService;

    @Autowired
    private RoleService roleService;

    @GetMapping(path = "/index")
    public String index(){
        return "Interview/InterviewProfiles";
    }

    @PostMapping(path = "/list")
    @ResponseBody
    public Map<String,Object> list(int page, int limit, Interview interview){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        int count=0;
        List<Interview> interviews= interviewService.findAll((page-1)*limit,limit,interview);
        //查询总条数(该名称下)
        map.put("count", interviews.size());
        map.put("data", interviews);
        return map;
    }

    /**
     * 添加需求
     */
    @GetMapping(path = "/insert")
    public ModelAndView insert() {
        ModelAndView mav = new ModelAndView("Interview/stinsert");
        //获取全部的需求
        List<Demand> demands=demandService.findAll();
        mav.addObject("demands",demands);
        return mav;
    }

    @PostMapping(path = "/insertsp")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> insertsp(Interview interview){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        //获取需求表的内容
        Demand demand=demandService.findByid(interview.getDemandid());
        int mianshi=demand.getDemand();
        interview.setEmpid(mianshi);
        interview.setStateid(10);
        int resule= interviewService.insertDem(interview);
        if (resule>0){
            map.put("msg","添加成功");
            map.put("data","success");
        }else {
            map.put("msg","添加失败");
            map.put("data","fail");
        }
        return map;
    }
    /**
     * 筛选
     */
    @PostMapping(path = "/saixuan")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> saixuan(){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        //查询所有的候选人
        List<Interview> interviews=interviewService.findOne();
        int a=0;
        for (int i = 0; i < interviews.size(); i++) {
            int cg=0;
            String xueli=interviews.get(i).getDegree();
            int nianxian=interviews.get(i).getLimit();
            //需求表的id
            int id=interviews.get(i).getDemand().getId();
            //根据需求表的id查询需求表的学历和年限
            Demand demand= demandService.findByid(id);
            if (demand.getDegree().equals(xueli)){
                cg++;
            }
            if (demand.getAgelimit()<=nianxian){
                cg++;
            }
            if (cg<2){
                //修改面试结果
                Interview interview=new Interview();
                interview.setId(interviews.get(i).getId());
                interview.setStateid(12);
                interviewService.updateDem(interview);
            }
            a++;
        }

        if (a>0){
            map.put("msg","筛选成功");
            map.put("data","success");
        }else {
            map.put("msg","筛选失败");
            map.put("data","fail");
        }
        return map;
    }
    /**
     * 面试结果审批
     * 点击审批按钮弹出信息页面
     * @param id
     * @return
     */
    @GetMapping(path = "/updemand")
    public ModelAndView updemand(int id) {
        ModelAndView mav = new ModelAndView("Interview/edit");
        //根据id查询面试申请表
        Interview interview = interviewService.findByid(id);
        mav.addObject("interview",interview);
        return mav;
    }

    /**
     * 面试结果审批添加
     * @param interview
     * @return
     */
    @PostMapping(path = "/updateint")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> updateint(Interview interview){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);

        //需求
        Demand demand = demandService.findByid(interview.getDemandid());

        int resule= interviewService.updateDem(interview);
        if (resule>0){
            //获取需求表id
            int xid = interview.getDemandid();
            //获取改编号的需求
            //获取人数
            int renid=demand.getDnumber();
            Demand demanda=new Demand();
            int num=renid-1;
            if (num>0){
                demanda.setDnumber(num);
                demanda.setId(xid);
                demandService.updateDem(demanda);
            }else{
                demanda.setState(11);
                demanda.setId(xid);
                demandService.updateDem(demanda);
            }
            if (interview.getStateid()==13){
                EmployeeInfo employeeInfo = new EmployeeInfo();
                //员工姓名
                employeeInfo.setEmpName(interview.getHxname());
                //员工手机号
                employeeInfo.setEmpPhone(interview.getHxphone());
                //部门ID
                employeeInfo.setDepartmentId(demand.getDepartmentname().getId());
                //学历
                employeeInfo.setEmpXueLi(demand.getDegree());
                //员工所属角色
                if (demand.getRoieid()!=0 && demand.getRoieid()!=null){
                    employeeInfo.setRoleId(demand.getRoieid());
                }else{
                    //根据新增的角色名称查询id
                    Role role = roleService.setByName(demand.getRoleadd());
                    employeeInfo.setRoleId(role.getId());
                }
                //添加员工档案
                employeeInfoService.insert(employeeInfo);
            }
            map.put("msg","审批成功");
            map.put("data","success");
        }else {
            map.put("msg","审批失败");
            map.put("data","fail");
        }
        return map;
    }
}
